﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="ImageControl.cs" company="">
//   
// </copyright>
// <summary>
//   The image control.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Libium.Controls
{
	using System;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Windows.Forms;
using Libium.Views;

    /// <summary>
    /// The image control.
    /// </summary>
    public partial class ImageControl : FileBaseControl, IImageView
    {
        #region Fields

        /// <summary>
        /// The _show text.
        /// </summary>
        private bool _showText = true;

        #endregion

        #region Constructors and Destructors

        /// <summary>
        /// Initializes a new instance of the <see cref="ImageControl"/> class.
        /// </summary>
        public ImageControl()
        {
            InitializeComponent();
            CaptureGlobalMouse = true;
            DragHandler.AddControl(imgBox);
            DropHandler.AddControl(imgBox);
            DropHandler.AddControl(NoImageControl);
				NoImageControl.BringToFront();
        }

        #endregion

        #region Public Properties

        public bool ShowToolTip
        {
            get
            {
                return ToolTip.Active;
            }

            set
            {
                ToolTip.Active = value;
            }
        }

		  public DockStyle ToolStripDockStyle
		  {
			  get
			  {
				  return toolStrip.Dock;
			  }
			  set
			  {
				  toolStrip.Dock = value;
				  toolStrip.BringToFront();
			  }
		  }

        /// <summary>
        /// Gets or sets the details text.
        /// </summary>
        public override string DetailsText
        {
            get
            {
                return ToolTip.GetToolTip(imgBox);
            }

            set
            {
                ToolTip.SetToolTip(imgBox, value);
            }
        }

        /// <summary>
        /// Gets or sets a value indicating whether show text.
        /// </summary>
        public bool ShowText
        {
            get
            {
                return _showText;
            }

            set
            {
                _showText = value;
            }
        }

        /// <summary>
        /// Gets or sets the text.
        /// </summary>
        public override string Text
        {
            get
            {
                return lblInformation.Text;
            }

            set
            {
                lblInformation.Text = value;
            }
        }

        #endregion

        #region Methods

        /// <summary>
        /// The on file path changed.
        /// </summary>
        protected override void OnFilePathChanged()
        {
            /*var filePath = FilePath;
            if (string.IsNullOrEmpty(filePath))
            {
					try
					{
						imgBox.ImageLocation = filePath;
						imgBox.SizeMode = (imgBox.Image.Width > imgBox.Width || imgBox.Image.Height > imgBox.Height)
							 ? PictureBoxSizeMode.Zoom : PictureBoxSizeMode.CenterImage;
						NoImageControl.Visible = false;
						return;
					}
					catch (ArgumentException)
					{
					}
					catch (InvalidOperationException)
					{
					}
					catch (FileNotFoundException)
					{
					}
					catch (Exception ex)
					{
						Console.WriteLine(ex.ToString());
					}
            }

            imgBox.Image = null;
            NoImageControl.Visible = true;*/
			  if (FilePath == null)
			  {
				  imgBox.Image = null;
				  NoImageControl.Visible = true;
			  }
			  else
			  {
				  imgBox.ImageLocation = FilePath;
				  NoImageControl.Visible = false;
			  }
			  if (!string.IsNullOrEmpty(imgBox.ImageLocation))
			  {
				  FileInfo file = new FileInfo(imgBox.ImageLocation);
				  var sizeInBytes = file.Length;

				  Bitmap img = new Bitmap(imgBox.ImageLocation);

				  var imageHeight = img.Height;
				  var imageWidth = img.Width;

				  imgBox.SizeMode = (imageWidth > imgBox.Width || imageHeight > imgBox.Height) ? System.Windows.Forms.PictureBoxSizeMode.Zoom
																														  : System.Windows.Forms.PictureBoxSizeMode.CenterImage;
			  }
        }

        public void RemoveToolStripItem(string buttonName)
		  {
			  toolStrip.Items.RemoveByKey(buttonName);
		  }

		 /*
        /// <summary>
        /// The on global mouse enter.
        /// </summary>
        protected override void OnGlobalMouseEnter()
        {
            base.OnGlobalMouseEnter();
            if (_showText)
            {
                pnlInformation.Visible = true;
            }
        }

        /// <summary>
        /// The on global mouse leave.
        /// </summary>
        protected override void OnGlobalMouseLeave()
        {
            base.OnGlobalMouseLeave();
            pnlInformation.Visible = false;
        }*/

        /// <summary>
        /// The image control_ resize.
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="e">
        /// The e.
        /// </param>
        private void ImageControl_Resize(object sender, EventArgs e)
        {
            lblInformation.MaximumSize = new Size(pnlInformation.Width, 0);
        }

        /// <summary>
        /// The img box_ double click.
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="e">
        /// The e.
        /// </param>
        private void imgBox_DoubleClick(object sender, EventArgs e)
        {
            if (EditButton.Enabled && EditButton.Visible)
            {
                EditButton.PerformClick();
            }
        }

		  public void PerformEdit()
		  {
			  EditButton.PerformClick();
		  }

        #endregion
    }
}
